package com.example.androidtvdemo.demo03;

import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.androidtvdemo.R;

import java.util.List;

public class List01Adapter extends BaseQuickAdapter<String, BaseViewHolder> {
    private static final String TAG = "List";
    private ItemListener itemListener;

    public void setOnListener(ItemListener listener) {
        this.itemListener = listener;
    }

    public List01Adapter() {
        super(R.layout.item_list_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        Button textView = helper.getView(R.id.tv_name);
        textView.setText(item);
        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.colorWhite));
        Log.i(TAG, "convert: 更新title");
        final int adapterPosition = helper.getAdapterPosition();
        //左边卡主不动,右边卡主不动,不往下跳
        if (adapterPosition == 0) {
            textView.setNextFocusLeftId(textView.getId());
        }
        if (adapterPosition == getData().size() - 1) {
            textView.setNextFocusRightId(textView.getId());
        }
        //如果是最后一个item才做焦点的监听
        if (itemListener != null && adapterPosition == getData().size() - 1) {
            //把当前的控件的焦点监听回传到act
            textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (itemListener != null) {
                        //往外通知监听焦点
                        itemListener.onHasFocus(v, adapterPosition, hasFocus);
                    }
                }
            });
        }
    }
}
